package com.yisu.expressway.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.model.ServiceDistrictObj;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.ui.SingleButtonAlertDialog;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

public class UserAdviceActivity extends BaseActivity {

    @Bind(R.id.edt_message_content)
    protected EditText mEdt_messageContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_advice);
        setTitle(R.string.user_response);
    }

    @OnClick(R.id.btn_upload)
    protected void uploadAdvice(){

        String msgContent = mEdt_messageContent.getText().toString();
        if(StringUtil.isNullOrEmpty(msgContent)){
            ToastUtil.displayShortToastMsg(UserAdviceActivity.this,R.string.content_empty_notify);
            return;
        }

        showLoading();

        Map<String, Object> params = new HashMap();
        params.put("content", msgContent);

        ApiClientHelper.postApi(ApiSetting.uploadUserAdvice(), new TypeToken<ServiceDistrictObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ServiceDistrictObj>>() {
            @Override
            public void onResponse(ApiResult<ServiceDistrictObj> result) {

                dismissLoading();
                if (!result.success) {
                    ToastUtil.displayLongToastMsg(UserAdviceActivity.this, result.getMsg());
                    return;
                }

                final SingleButtonAlertDialog dialog = new SingleButtonAlertDialog(UserAdviceActivity.this);
                dialog.show();
                dialog.setContentText(R.string.upload_message_success);
                dialog.setPositiveClickListener(new SingleButtonAlertDialog.PositiveClickListener() {
                    @Override
                    public void onPositiveClick() {
                        dialog.dismiss();
                        finish();
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                ToastUtil.displayShortToastMsg(UserAdviceActivity.this, volleyError.getMessage());
            }
        }, this);
    }
}
